/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package commonClasses.Messages;

import commonClasses.*;
import commonClasses.commonInterfaces.MsgIntf;
import commonClasses.commonInterfaces.ProtocolIntf;

/**
 *
 * @author Pozytywny!
 */
public class LoginMsg extends Msg implements MsgIntf{

    private String _hash = null;   //md5
    private int _version = 0;    //

    private final int HASH_LENGTH = 32;


    public LoginMsg(ProtocolIntf protocol, String hash, int version) {
        super(protocol, MagicNumbers.LOGIN_MSG_NR);
        this._hash = hash;
        this._version = version;
    }

    public LoginMsg(ProtocolIntf protocol) {
        super(protocol, MagicNumbers.LOGIN_MSG_NR);
    }

    public void receive() throws Exception {
        _version = receive16uint();
        Utils.hostReceived("HOST version:"+_version);

        _hash = String.valueOf(receiveCharArr(HASH_LENGTH));
        Utils.hostReceived("HASH hash:"+_hash);
    }

    public void send() throws Exception {
        int mesgId[] = this.idToIntArr();
        sendIntArr(mesgId);

        int versionArr[] = intToArr(_version);
        sendIntArr(versionArr);

        char hash[] = _hash.toCharArray();
        Utils.write("Hash:"+_hash);
        if(hash.length!=HASH_LENGTH) Utils.write("długość hasha jest != 32, " + hash.length);
        sendCharArr(hash,HASH_LENGTH);
        
    }

    public String getHash() {
        return _hash;
    }

    public int getMagicNumber() throws Exception{
        if(_version == 0) throw new Exception("_version not set!");
        return (int) (_version & 0xF0) >> 8;
    }

    public int getVersionNumber() throws Exception{
        if(_version == 0) throw new Exception("_version not set!");
        return (int) (_version &0xF);
    }
}
